var hourreg = /^([0|1]?[0-9]{1})$/;
var hourreg2 = /^(2[0|1|2|3])$/
var minutereg = /^[0-5]?[0-9]$/;

/**
 * 将时间字符串解析成时间
 */
function parseTime(timestr) {
	var times = timestr.split(":",2);
	var hour,minute;
	if(times.length < 2) {
		hour = minute = 0;
	} else {
		hour = parseInt(times[0],10);
		minute = parseInt(times[1],10);
	}
	return {
		hour:hour,
		minute:minute
	};
}

/**
 * opts :{input,onchange}
 * input			输入控件
 * onchange:		时间指改变时触发该事件
 */
function TimeInput(opts) {
	
	//默认值
	this.defaultVal = opts.input.attr("value");
	
	this.wrapper = $(TimeInput.WRAPPER);
	
	//表单隐藏域
	var name = opts.input.attr("name"); 
	this.hiddenInput = $("<input type='hidden' name='"+name+"' value='"+this.defaultVal+"'/>");
	this.wrapper.append(this.hiddenInput);
	
	
	opts.input.replaceWith(this.wrapper);
	
	this.hour =this.wrapper.find("> .hour");
	this.minute =this.wrapper.find("> .minute");

	this.hourtext = this.hour.find("> .hourtext");
	this.minutetext = this.minute.find("> .minutetext");

	this.hourval = "";
	this.minuteval = "";
	

	//设置默认值
	if(this.defaultVal != "") {
		this.setValue(this.defaultVal);
	}

	//值改变事件
	if(opts.onchange){
		this.onchange = opts.onchange;
	}
	
	//////////////////////////////////设置事件//////////////////////////////////
	
	var me = this;

	//小时输入调整
	this.hour.find("> .control > .up").click( function() {
		var hour = parseInt(me.getHour(),10);
		me.setHour(++hour);
	});
	this.hour.find("> .control > .down").click( function() {
		var hour = parseInt(me.getHour(),10);
		me.setHour(--hour);
	});
	//分钟输入调整
	this.minute.find("> .control > .up").click( function() {
		var minute = parseInt(me.getMinute());
		me.setMinute(++minute);
	});
	this.minute.find("> .control > .down").click( function() {
		var minute = parseInt(me.getMinute());
		me.setMinute(--minute);
	});
	//小时输入
	this.hourtext.focus( function() {
		$(this).get(0).select();
		var values = [];
		for(var i = 0;i<10;i++) {
			values.push("0"+i);
		}
		for(var i = 10;i<24;i++) {
			values.push(""+i);
		}
		me.hourSuggest(values);
	}).keyup( function() {
		var value = $(this).attr("value");
		if(value == "") {
			me.clearHourSuggest();
			return false;
		}

		if(hourreg.test(value) || hourreg2.test(value)) {
			me.hourval = value;
		}
		//$(this).attr("value",me.hourval);/////////////////////////////////////////
		me.setHour(me.hourval);

		if(me.hourval.length ==1) {//自动提示
			var intval = parseInt(me.hourval);
			var values = [];
			if(intval == 0) {
				for(var i = 0;i<10;i++) {
					values.push("0"+i);
				}
			} else if(intval == 1) {
				values.push("01");
				for(var i = 0;i<10;i++) {
					values.push("1"+i);
				}
			} else if(intval == 2) {
				values.push("02");
				for(var i = 0;i<4;i++) {
					values.push("2"+i);
				}
			} else {
				values.push("0"+intval);
			}
			me.hourSuggest(values);
		} else {
			me.updateHiddenInput();
			me.clearHourSuggest();
		}

	});
	//分钟输入
	this.minutetext.focus( function() {
		$(this).get(0).select();
		var values = [];
		values.push("00");
		for(var i = 10;i<60;i+=10) {
			values.push(""+i);
		}
		me.minuteSuggest(values);
	}).keyup( function() {
		var value = $(this).attr("value");
		if(value == "") {
			me.clearMinuteSuggest();
			return false;
		}

		if(minutereg.test(value)) {
			me.minuteval = value;
		}
		//$(this).attr("value",me.minuteval);
		me.setMinute(me.minuteval);
		
		if(me.minuteval.length ==1) {//自动提示
			var intval = parseInt(me.minuteval);
			var values = [];
			if(intval == 0) {
				for(var i = 0;i<10;i++) {
					values.push("0"+i);
				}
			} else if(0 < intval && intval < 6) {
				values.push("0"+intval);
				for(var i = 0;i<10;i++) {
					values.push(""+intval+i);
				}
			} else {
				values.push("0"+intval);
			}
			me.minuteSuggest(values);
		} else {
			me.updateHiddenInput();
			me.clearMinuteSuggest();
		}
	});
	//上下键
	this.hourtext.keypress( function(event) {
		if(event.keyCode == 38) {
			var hour = parseInt(me.getHour(),10);
			me.setHour(++hour);
		} else if(event.keyCode == 40) {
			var hour = parseInt(me.getHour(),10);
			me.setHour(--hour);
		}
	});
	this.minutetext.keypress( function(event) {
		if(event.keyCode == 38) {
			var minute = parseInt(me.getMinute());
			me.setMinute(++minute);
		} else if(event.keyCode == 40) {
			var minute = parseInt(me.getMinute());
			me.setMinute(--minute);
		}
	});
	//滚轮事件
	this.hourtext.mousewheel( function(objEvent, intDelta) {
		var hour = parseInt(me.getHour(),10);
		if (intDelta > 0) {
			me.setHour(++hour);
		} else if (intDelta < 0) {
			me.setHour(--hour);
		}
	});
	this.minutetext.mousewheel( function(objEvent, intDelta) {
		var minute = parseInt(me.getMinute());
		if (intDelta > 0) {
			me.setMinute(++minute);
		} else if (intDelta < 0) {
			me.setMinute(--minute);
		}
	});
}

TimeInput.WRAPPER = '<div class="timeinput">'+
				'<div class="hour">'+
					'<input type="text" class="hourtext"/>'+
					'<div class="control">'+
						'<a href="javascript:;" class="up">+</a>'+
						'<a href="javascript:;" class="down">-</a>'+
					'</div>'+
				'</div>'+
				'<div class="seperator">:</div>'+
				'<div class="minute">'+
					'<input type="text" class="minutetext"/>'+
					'<div class="control">'+
						'<a href="javascript:;" class="up">+</a>'+
						'<a href="javascript:;" class="down">-</a>'+
					'</div>'+
				'</div>'+
			'</div>';


/**
 * 更新隐藏域的值
 */
TimeInput.prototype.updateHiddenInput = function() {
	if(this.hiddenInput) {
		this.hiddenInput.attr("value",this.getHour()+":"+this.getMinute());
	}
}
/**
 * 清楚分钟输入提示
 */
TimeInput.prototype.clearMinuteSuggest = function() {
	if(this.minuteAutoComplete) {
		this.minuteAutoComplete.hide().empty();
	}
}
/**
 * 显示分钟输入提示
 */
TimeInput.prototype.minuteSuggest = function(values) {
	if(!this.minuteAutoComplete) {
		this.minuteAutoComplete = $("<ul class='suggestlist'></ul>");
		this.minute.append(this.minuteAutoComplete);
		this.minuteAutoComplete.hide();
	}
	this.minuteAutoComplete.empty();
	var listr = "";
	for(var i = 0;i<values.length;i++) {
		listr += "<li>"+values[i]+"</li>";
	}
	this.minuteAutoComplete.append(listr);
	var me = this;
	this.minuteAutoComplete.find("> li").click( function() {
		var value = $(this).html();
		//me.minutetext.attr("value",value);
		me.setMinute(value);
		me.clearMinuteSuggest();
	});
	this.minuteAutoComplete.show();
}
/**
 * 清楚小时输入提示
 */
TimeInput.prototype.clearHourSuggest = function() {
	if(this.hourAutoComplete) {
		this.hourAutoComplete.hide().empty();
	}
}
/**
 * 显示小时输入提示
 */
TimeInput.prototype.hourSuggest = function(values) {
	if(!this.hourAutoComplete) {
		this.hourAutoComplete = $("<ul class='suggestlist'></ul>");
		this.hour.append(this.hourAutoComplete);
		this.hourAutoComplete.hide();
	}
	this.hourAutoComplete.empty();
	var listr = "";
	for(var i = 0;i<values.length;i++) {
		listr += "<li>"+values[i]+"</li>";
	}
	this.hourAutoComplete.append(listr);
	var me = this;
	this.hourAutoComplete.find("> li").click( function() {
		var value = $(this).html();
		//me.hourtext.attr("value",value);
		me.setHour(value);
		me.clearHourSuggest();
	});
	this.hourAutoComplete.show();
}
/**
 *	 获取小时
 */
TimeInput.prototype.getHour = function() {
	return this.hourtext.attr("value");
}
/**
 * 设置小时
 */
TimeInput.prototype.setHour = function(value) {
	if(typeof value == 'string'){
		value = parseInt(value,10);
	}
	if(isNaN(value) || value > 23)
		value = 0;
	if(value < 0 )
		value = 23;
	if(value < 10) {
		value = "0"+value;
	} else {
		value = ""+value;
	}
	this.hourtext.attr("value",value);
	this.updateHiddenInput();
	this.clearHourSuggest();
}
/**
 * 获取分钟
 */
TimeInput.prototype.getMinute = function() {
	return this.minutetext.attr("value");
}
/**
 * 设置分钟
 */
TimeInput.prototype.setMinute = function(value) {
	if(typeof value == 'string'){
		value = parseInt(value,10);
	}
	if(isNaN(value) || value > 59)
		value = 0;
	if(value < 0 )
		value = 59;
	if(value < 10) {
		value = "0"+value;
	} else {
		value = ""+value;
	}
	this.minutetext.attr("value",value);
	this.updateHiddenInput();
	this.clearMinuteSuggest();
}
/**
 * 重置
 */
TimeInput.prototype.reset = function() {
	this.setHour(0);
	this.setMinute(0);
}

/**
 * 设置时间
 */
TimeInput.prototype.setValue = function(strValue) {
	var time = parseTime(strValue);
	this.setHour(time.hour);
	this.setMinute(time.minute);
}
/**
 * 获取时间
 */
TimeInput.prototype.getValue = function() {
	var hour = this.getHour();
	var minute = this.getMinute();
	if(hour == "" || minute == "")
		return null;
	else
		return hour +":"+minute;
}